package design.pattern.Lu15CompositePattern;

import lombok.Data;

import java.util.*;

/**
 * @program JavaBase
 * @description: 组合模式-演练
 *  树形菜单
 * @author: zhanglu
 * @create: 2020-04-09 19:13:00
 */
public class CompositePattern02 {

    private static List<User> userList = new ArrayList<>();

    static {

        userList.add(new User(1L, "CEO", "总经理", false, null));
        userList.add(new User(2L, "developDep", "研发部经理", false, "CEO"));
        userList.add(new User(3L, "salesDep", "销售部经理", false, "CEO"));
        userList.add(new User(4L, "financeEep", "财务部经理", false, "CEO"));
        userList.add(new User(5L, "k", "总经理秘书", true, "CEO"));
        userList.add(new User(6L, "a", "员工A", true, "developDep"));
        userList.add(new User(7L, "b", "员工B", true, "salesDep"));
    }

    public static void main(String[] args) {

        UserTree root = new UserTree();

        Map<String, List<UserTree>> childUserTreeList = new HashMap<>();

        for (User user : userList) {
            if(user.getParentCode() != null){
                if(user.isLeaf()){
                    UserTree leaf = new UserTree();
                    leaf.setInfo(user);
                    childUserTreeList.get(user.getParentCode()).add(leaf);
                }else {
                    UserTree branch = new UserTree();
                    branch.setInfo(user);
                    childUserTreeList.get(user.getParentCode()).add(branch);
                    childUserTreeList.put(user.getCode(), new ArrayList<>());
                }
            }else {
                root.setInfo(user);
                childUserTreeList.put(user.getCode(), new ArrayList<>());
            }
        }

        show(root);

    }

    public static void show(UserTree userTree){



    }

}

abstract class Tree<T>{

    protected T t;

    protected void setInfo(T t){
        this.t = t;
    }

    protected void getInfo(T t){
        System.out.println(t.toString());
    }

}

class UserTree extends Tree<User>{

    private List<Tree> treeList = new ArrayList<>();

    public void add(UserTree user){
        this.treeList.add(user);
    }

    public List<Tree> getTreeList(){
        return this.treeList;
    }

}

@Data
class User extends Tree<User>{

    private Long id;
    private String code;
    private String name;
    private boolean isLeaf;
    private String parentCode;

    public User(Long id, String code, String name, boolean isLeaf, String parentCode) {
        this.id = id;
        this.code = code;
        this.name = name;
        this.isLeaf = isLeaf;
        this.parentCode = parentCode;
    }

}
